//
//  RBListSelectVC.m
//  WaitSeller
//
//  Created by Lan on 2018/11/13.
//  Copyright © 2018年 SummerTea. All rights reserved.
//

#import "RBListSelectVC.h"
#import "RBDefine.h"
#import "RBThemeConfig.h"
#import <Masonry.h>

@interface RBListSelectCell: UITableViewCell

@property(nonatomic, strong) UILabel *textL;
@property(nonatomic, copy) NSString *text;

@end

@implementation RBListSelectCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

#pragma mark - Setter

- (void)setText:(NSString *)text
{
    _text = text;
    self.textL.text = text;
}

#pragma mark - Getter

- (UILabel *)textL
{
    if (!_textL)
    {
        self.textL = ({
            UILabel *view = [UILabel new];
            view.textColor = [RBThemeConfig textBlackColor];
            view.font = [UIFont systemFontOfSize:13];
            view.numberOfLines = 1;
            view.textAlignment = NSTextAlignmentCenter;
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _textL;
}

@end

@interface RBListSelectVC ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) UIView *containerView;
@property(nonatomic, strong) UIImageView *bubbleView;

@property(nonatomic, strong) UITableView *tableview;
@property(nonatomic, strong) NSArray *dataSource;

@property(nonatomic, copy) void(^block)(NSInteger index,NSString *text);

@end

@implementation RBListSelectVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.05];
}

- (void)dismiss
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark - Public

- (void)showFromVC: (UIViewController *)vc
             point: (CGPoint)point
             width: (CGFloat)width
         maxHeight: (CGFloat)height
              data: (NSArray *)data
        completion: (void(^)(NSInteger index,NSString *text))completion;
{
    self.providesPresentationContextTransitionStyle = YES;
    self.definesPresentationContext = YES;
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [vc presentViewController:self animated:NO completion:nil];
    
    self.dataSource = data;
    self.block = completion;
    
    CGFloat maxHeight = kRBMainScreenHeight - kRBSafeBottomHeight - point.y - 25/** 底部留白 */;
    if (height > 0)
    {
        maxHeight = MIN(maxHeight, height);
    }
    CGFloat containerH = 35*self.dataSource.count;
    containerH = MIN(containerH, maxHeight);
    self.containerView.frame = ({
        CGFloat w = width;
        CGFloat y = point.y;
        CGFloat h = containerH;
        CGFloat x = point.x;
        CGRectMake(x, y, w, h);
    });
    self.bubbleView.hidden = NO;
    self.tableview.hidden = NO;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RBListSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.text = self.dataSource[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.block)
    {
        self.block(indexPath.row, self.dataSource[indexPath.row]);
    }
    [self dismiss];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

#pragma mark - Event

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = touches.anyObject;
    CGPoint point = [touch locationInView:self.view];
    if (!CGRectContainsPoint(self.containerView.frame, point))
    {
        [self dismiss];
    }
}

#pragma mark - Getter

- (UIView *)containerView
{
    if (!_containerView)
    {
        self.containerView = ({
            UIView *view = [UIView new];
            view.backgroundColor = [UIColor clearColor];
            [self.view addSubview:view];
            view;
        });
    }
    return _containerView;
}

- (UIImageView *)bubbleView
{
    if (!_bubbleView)
    {
        self.bubbleView = ({
            UIImageView *view = [UIImageView new];
            view.clipsToBounds = YES;
            view.userInteractionEnabled = YES;
            [self.containerView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _bubbleView;
}

- (UITableView *)tableview
{
    if (!_tableview) {
        self.tableview = ({
            UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
            tableview.dataSource = self;
            tableview.delegate = self;
            tableview.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
            [tableview registerClass:[RBListSelectCell class] forCellReuseIdentifier:@"cell"];
            tableview.showsVerticalScrollIndicator = NO;
            tableview.showsHorizontalScrollIndicator = NO;
            tableview.tableFooterView = [UIView new];
            [self.bubbleView addSubview:tableview];
            [tableview mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
            }];
            tableview;
        });
    }
    return _tableview;
}

@end
